var VipUserTable = {
    id: "vipUserTable", //表格id
    seItem: null,		//选中的条目
    me: ""
};

/**
 * 初始化整个表格
 */
VipUserTable.init = function () {
    var defaultColunms = this.initColumn();
    var bstable = new BSTable(VipUserTable.id, Feng.ctxPath + "/user/pageParam", defaultColunms);
    // bstable.setData({});
    this.me = bstable.init();
};

/**
 * 初始化表格的列
 */
VipUserTable.initColumn = function () {
    var columns = [
        {
            title: '序号', align: 'center', valign: 'middle',
            formatter: function indexFormatter(value, row, index) {//在表格前加上序号
                return index + 1;
            }
        },
        {
            title: '头像', field: 'headImgUrl', align: 'center', valign: 'middle',
            formatter: function (value, row, index) {
                if (value == null){
                     value = Feng.ctxPath+'/img/vipuser.png';
                    return '<img style="width: 50px;height: 50px;" onclick="showPic(\'' + value + '\');" src="' + value + '" class="img-rounded" >';
                }else{
                    return '<img style="width: 50px;height: 50px;" onclick="showPic(\'' + value + '\');" src="' + value + '" class="img-rounded" >';
                }
            }
        },
        {title: '用户ID', field: 'id', align: 'center', valign: 'middle'},
        {title: '用户名', field: 'name', align: 'center', valign: 'middle'},
        {
            title: '性别', field: 'sex', align: 'center', valign: 'middle',
            formatter: function (value, row, index) {
                if (value === "0") {
                    return "未知";
                } else if (value === "1") {
                    return "男";
                } else if (value === '2') {
                    return "女";
                }
            }
        },
        {title: '家长名称', field: 'parentsName', align: 'center', valign: 'middle'},
        {
            title: '注册方式', field: 'regType', align: 'center', valign: 'middle',
            formatter: function (value, row, index) {
                if (value === "WeChat") {
                    return "微信";
                } else if (value === "APP") {
                    return "APP注册";
                } else if (value === 'BackGround') {
                    return "后台";
                }
            }
        },
        {title: '注册时间', field: 'createDate', align: 'center', valign: 'middle'},
        {
            title: '操作', field: '#', align: 'center', valign: 'middle',
            formatter: function (value, row, index) {
                // var d = '<button class="btn btn-primary btn-xs" onclick="viewItems(\''+ row.id + '\')" title="查看详情"><i class="glyphicon glyphicon-align-justify"></i></button> &nbsp;&nbsp;';
                var e = '<button class="btn btn-primary btn-xs" onclick="editBtn(\'' + row.id + '\')" title="编辑"><i class="glyphicon glyphicon-pencil"></i></button> &nbsp;&nbsp;';
                var f = '<button class="btn btn-danger btn-xs" onclick="deleteBtn(\'' + row.id + '\',\'' + row.name + '\')" title="删除"><i class="glyphicon glyphicon-trash"></i></button>';
                return e + f;
            }
        }];
    return columns;
};

/**
 * 点击搜索按钮
 * @returns
 */
function clickSearch() {
    var queryData = {};
    var val_search_name = $("#txt_search_name").val();

    if (val_search_name !== "" || val_search_name !== null) {
        queryData['name'] = val_search_name;
    }
    VipUserTable.me.destroy(queryData);
    // VipUserTable.me.refresh({query: queryData});
}

/**
 * 点击编辑按钮时，跳出用户详情对话框
 * @returns
 */
function editBtn(uid) {
    var index = layer.open({
        type: 1,
        title: '编辑会员信息',
        skin: 'layui-layer-lan',
        area: [VipUserInfoDlg.width, 'auto'],
        content: VipUserInfoDlg.cache,
        success: function (layero, index) {
            //层弹出成功后回调方法
            VipUserInfoDlg.renderAdd();
            VipUserInfoDlg.load(uid);
        },
        maxmin: true,
        scrollbar: false,
        btn: ['保存', '关闭'],
        btnAlign: 'r',  //默认值
        yes: function (index, layero) {
            VipUserInfoDlg.editSubmit();
            layer.close(index); //如果设定了yes回调，需进行手工关闭
        }, btn2: function (index) {
            layer.close(index);
        },
        cancel: function (index, layero) {
            //右上角关闭按钮触发的回调
            layer.close(index);
        }
    });
    VipUserInfoDlg.currentLayerIndex = index;
}


/**
 * 删除某个会员
 * @param uid 用户的id
 * @returns
 */
function deleteBtn(uid, deleteName) {
    layer.confirm('是否删除用户:' + deleteName, {icon: 10, title: '是否删除用户?'}, function (index) {
        var ajax = new $ax(Feng.ctxPath + "/user/deleteById", function (data) {
            if (data.success) {
                Feng.success("删除成功!");
                VipUserTable.me.refresh();
                layer.close(index);
            } else if (data.code === 2) {
                Feng.fail(data.msg);
            } else {
                Feng.fail("删除失败!");
            }
        }, function (data) {
            Feng.error("删除失败!");
        });
        ajax.data = {id: uid};
        ajax.start();
    });
}

/**
 * 查看会员详情
 * @returns
 */
function viewItems(uid) {
    var index = layer.open({
        type: 1,
        title: '会员详情',
        skin: 'layui-layer-lan', //加上边框
        area: [VipUserInfoDlg.width, 'auto'],//宽高
        content: VipUserInfoDlg.cache,
        success: function (layero, index) {
            VipUserInfoDlg.renderAdd();
            VipUserInfoDlg.load(uid);
        },
        maxmin: true,
        scrollbar: false,
        cancel: function (index, layero) {
            //右上角关闭按钮触发的回调
            layer.close(index);
        }
    });
    VipUserInfoDlg.currentLayerIndex = index;
}

/**
 * 导出会员信息
 */
$("#btn_excel_export").on("click",function () {
    var url = Feng.ctxPath + "/api/user/exportUser"
    window.location = url;
});


$(function () {
    //初始化会员管理表格
    VipUserTable.init();
    //初始化会员详情
    VipUserInfoDlg.init();
});
